package com.gx.smart.building.ui.swap.viewmodel

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.gx.smart.building.admin.ScheduleAppApiService
import com.gx.smart.building.ui.common.viewmodel.CommonViewModel
import com.gx.smart.common.util.AppConfig
import com.gx.smart.lib.http.api.asynctask.CallBack
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleContentResp
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleModelQueryResp
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleRecordPlusResp
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleRecordResp
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleTime
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleTimeQueryResp
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.RecordDto

class StartSwapViewModel(app: Application) : CommonViewModel(app) {
    val content: MutableLiveData<RecordDto> = MutableLiveData()
    val contentSuccess: MutableLiveData<Boolean> = MutableLiveData()
    val history: MutableLiveData<RecordDto> = MutableLiveData()
    val check: MutableLiveData<Int> = MutableLiveData()
    private val scheduleTimeList: MutableLiveData<List<AppScheduleTime>> = MutableLiveData()
    private val scheduleDeviceStatus: MutableLiveData<Map<String, String>> = MutableLiveData()
    private val draftSuccess: MutableLiveData<Boolean> = MutableLiveData()
    private val id: MutableLiveData<String> = MutableLiveData()


    //发起交接——获取当前登录用户的交接页面
    fun queryAppScheduleModel(): LiveData<Int> {
        val userId = SPUtils.getInstance().getString(AppConfig.ID)
        ScheduleAppApiService.getInstance().queryAppScheduleModel(
            userId,
            object : CallBack<AppScheduleModelQueryResp>() {
                override fun callBack(result: AppScheduleModelQueryResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        contentSuccess.value = false
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        contentSuccess.value = true
                        //是否需要提示 1不需要（没有历史未提交直接进入使用content） 2需要（让客户自行判断使用content还是history）
                        content.value = result.content
                        history.value = result.history
                        check.value = result.check
                    } else {
                        handleServiceException(comResp)
                        contentSuccess.value = false
                    }
                }
            })
        return check
    }

    //发起交接——查询该用户，对应班次下的可选择班时（返回对应班时和该班时在该交接班模板下的交接时间范围）
    fun queryAppScheduleTimeListByUser(modelId: String): LiveData<List<AppScheduleTime>> {
        val userId = SPUtils.getInstance().getString(AppConfig.ID)
        ScheduleAppApiService.getInstance().queryAppScheduleTimeListByUser(userId, modelId,
            object : CallBack<AppScheduleTimeQueryResp>() {
                override fun callBack(result: AppScheduleTimeQueryResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        scheduleTimeList.value = result.contentList
                    } else {
                        handleServiceException(comResp)
                    }
                }
            })
        return scheduleTimeList
    }

    //发起交接——获取读数
    fun queryAppScheduleContent(ids: List<String>): LiveData<Map<String, String>> {
        ScheduleAppApiService.getInstance().queryAppScheduleContent(ids,
            object : CallBack<AppScheduleContentResp>() {
                override fun callBack(result: AppScheduleContentResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        scheduleDeviceStatus.value = result.contentMap
                    } else {
                        handleServiceException(comResp)
                    }
                }
            })
        return scheduleDeviceStatus
    }

    //发起交接——断点记录——填写完未提交，返回了前一页之前的操作
    fun noteAppScheduleRecord(content: RecordDto): LiveData<Boolean> {
        ScheduleAppApiService.getInstance().noteAppScheduleRecord(
            content,
            object : CallBack<AppScheduleRecordResp>() {
                override fun callBack(result: AppScheduleRecordResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        draftSuccess.value = false
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        draftSuccess.value = true
                    } else {
                        handleServiceException(comResp)
                        draftSuccess.value = false
                    }
                }

            })
        return draftSuccess
    }


    //交接班动作——交接单新增（返回成功与否，并附带一个二维码）
    fun insertAppScheduleRecord(content: RecordDto): LiveData<String> {
        ScheduleAppApiService.getInstance().insertAppScheduleRecord(
            content,
            object : CallBack<AppScheduleRecordPlusResp>() {
                override fun callBack(result: AppScheduleRecordPlusResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        id.value = result.id
                    } else {
                        handleServiceException(comResp)
                    }
                }
            })
        return id
    }


}